<?php

namespace app\api\controller;

use app\admin\model\Admin;
use app\admin\model\Log;
use think\exception\DbException;
use think\facade\Request;

/**
 * Class SystemLog
 * @package app\api\controller
 */
class SystemLog extends Common
{
    /**
     * @param Request $request
     * @return \think\response\Json
     */
    public function index(Request $request)
    {
        $where = [];
        if ($request::param('maker')) {
            $where[] = ['maker', '=', $request::param('maker')];
        }
        if ($request::param('start_time') && ($request::param('end_time'))) {
            $where[] = ['create_time', 'BETWEEN', [
                strtotime($request::param('start_time')),
                strtotime($request::param('end_time'))
            ]];
        }
        try {
            return $this->returnJson(200, 'ok', Log::where($where)->order('create_time desc')->page(
                $request::param('page', 1),
                $request::param('size', 10)
            )->paginate($request::param('size', 10))->each(function ($v) {
                $v['maker_name'] = Admin::find($v['maker'])['name'];
                $v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                return $v;
            }));
        } catch (DbException $e) {
            return $this->returnJson(500, $e->getMessage());
        }
    }
}
